package com.moxiao.yfjh.module.blind.service.background;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.module.blind.controller.admin.background.vo.BackgroundCreateReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.background.vo.BackgroundExportReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.background.vo.BackgroundPageReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.background.vo.BackgroundUpdateReqVO;
import com.moxiao.yfjh.module.blind.convert.background.BackgroundConvert;
import com.moxiao.yfjh.module.blind.dal.dataobject.background.BackgroundDO;
import com.moxiao.yfjh.module.blind.dal.mysql.background.BackgroundMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static com.moxiao.yfjh.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.moxiao.yfjh.module.blind.enums.ErrorCodeConstants.BACKGROUND_NOT_EXISTS;

/**
 * 背景图管理 Service 实现类
 *
 * @author mx
 */
@Service
@Validated
public class BackgroundServiceImpl implements BackgroundService {

    @Resource
    private BackgroundMapper backgroundMapper;

    @Override
    public Long createBackground(BackgroundCreateReqVO createReqVO) {
        // 插入
        BackgroundDO background = BackgroundConvert.INSTANCE.convert(createReqVO);
        backgroundMapper.insert(background);
        // 返回
        return background.getId();
    }

    @Override
    public void updateBackground(BackgroundUpdateReqVO updateReqVO) {
        // 校验存在
        validateBackgroundExists(updateReqVO.getId());
        // 更新
        BackgroundDO updateObj = BackgroundConvert.INSTANCE.convert(updateReqVO);
        backgroundMapper.updateById(updateObj);
    }

    @Override
    public void deleteBackground(Long id) {
        // 校验存在
        validateBackgroundExists(id);
        // 删除
        backgroundMapper.deleteById(id);
    }

    private void validateBackgroundExists(Long id) {
        if (backgroundMapper.selectById(id) == null) {
            throw exception(BACKGROUND_NOT_EXISTS);
        }
    }

    @Override
    public BackgroundDO getBackground(Long id) {
        return backgroundMapper.selectById(id);
    }

    @Override
    public List<BackgroundDO> getBackgroundList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return backgroundMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BackgroundDO> getBackgroundPage(BackgroundPageReqVO pageReqVO) {
        return backgroundMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BackgroundDO> getBackgroundList(BackgroundExportReqVO exportReqVO) {
        return backgroundMapper.selectList(exportReqVO);
    }

    @Override
    public String getRandomBackground(Integer type) {
        return backgroundMapper.getRandomBackground(type);
    }

}
